package com.cloud.drore.eboss.supplier.mapper.provider;

import com.cloud.drore.eboos.common.entity.supplier.ProductRelation;
import com.cloud.drore.eboss.supplier.entity.po.ProductRelationPo;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/9 10:22 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class ProductRelationProvider {

    private static final String FIND_CONSUME_POI_SQL = "select consume.id as consumeId,consume.consume_poi_name as poiName," +
            "consume.consume_poi_id as poiId,consume.consume_name as consumeName,merchant.id as merchantId,merchant.merchant_name as merchantName " +
            "from eboos_product product left join eboos_product_consume_relation productRelation on product.id = productRelation.product_id " +
            "left join eboos_consume consume on consume.id = productRelation.consume_id " +
            "left join eboos_merchant merchant on consume.merchant_id = merchant.id " +
            "where product.product_num =  ";


    public ProductRelationProvider(){

    }

    public String findByParam(ProductRelationPo productRelation){
        String sql = "select product_name,product_id,consume_id,crowd,product_code from eboos_product relation " +
                " where is_deleted = 0 ";
        if(productRelation.getConsumeId() != null)
            sql += " and relation.consume_id =" + productRelation.getConsumeId() + "";
        if(StringUtils.isNotBlank(productRelation.getProductName()))
            sql += " and relation.product_name ='" + productRelation.getProductName() + "'";
        if(StringUtils.isNotBlank(productRelation.getProductType()))
            sql += " and relation.product_type ='" + productRelation.getProductType() + "'";
        if(productRelation.getProductId() != null)
            sql += " and relation.product_id =" + productRelation.getProductId() + "";
        return sql;
    }

    public String updateProductByParam(ProductRelation relation){
        int count = 0;
        String sql = " update eboos_product set ";
        if (StringUtils.isNotBlank(relation.getStockMethod())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " stock_method ='" + relation.getStockMethod() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getRefundModel())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_model ='" + relation.getRefundModel() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getRefundFee())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_fee ='" + relation.getRefundFee() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getStatus())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " status ='" + relation.getStatus() + "'";
            count++;
        }
        if (relation.getIsDeleted()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_deleted ='" + relation.getIsDeleted() + "'";
            count++;
        }
        if (relation.getIsRealName()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_real_name ='" + relation.getIsRealName() + "'";
            count++;
        }
        if (relation.getSupplierSettlementPrice()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " supplier_settlement_price ='" + relation.getSupplierSettlementPrice() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getCrowd())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " crowd ='" + relation.getCrowd() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getPaymentType())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " payment_type ='" + relation.getPaymentType() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getRefundModel())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " refund_model ='" + relation.getRefundModel() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getInstruction())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " instruction ='" + relation.getInstruction() + "'";
            count++;
        }
        if (StringUtils.isNotBlank(relation.getConsumeId())) {
            if (count != 0) {
                sql += ",";
            }
            sql += " consume_id ='" + relation.getConsumeId() + "'";
            count++;
        }
        sql += " where product_id='" + relation.getProductId() + "' and product_type ='" + relation.getProductType() + "'";
        return sql;
    }

    /**
     * @ClassName: ProductRelationProvider
     * @param productNum
     * @Return: java.lang.String
     * @Decription:获取poi和消费点的sql
     * @CreateDate: Created in 2018/3/22 11:55
     * @Author: <a href="747639122@qq.com">冯腾</a>
     * @Modify:
     */
    public String findPoiAndConsumeForDealer(String productNum){
        String sql = FIND_CONSUME_POI_SQL;
        return sql += "'"+productNum+"'";
    }

}